package org.zebra.logging.config;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zebra.logging.aspect.LogAspect;
import org.zebra.logging.interceptor.HttpLogInterceptor;
import org.zebra.logging.util.LogScheduleUtil;

@AutoConfiguration
@Slf4j
public class LoggingConfig implements WebMvcConfigurer {

    @Autowired
    private Environment environment;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (Boolean.parseBoolean(environment.getProperty("zebra.log.http.enabled", "false"))) {
            registry.addInterceptor(new HttpLogInterceptor(SpringUtil.getBean(PathMatcher.class)));
        }
        if (Boolean.parseBoolean(environment.getProperty("zebra.log.cleaned", "false"))) {
            LogScheduleUtil.start();
        }
    }

    @Bean
    public LogAspect logAspect() {
        return new LogAspect();
    }
}
